Ambient Element
Element is a React-inspired virtual tree library for the Ambient runtime.
It is backed by the Ambient ECS; the virtual tree is converted into a real tree of entities and components. When the tree is updated, it is compared to the previous tree, and only the differences are applied to the ECS. This can be used for UI, as well as any other tree-like data structure that you want to be able to update efficiently.
The Element module is closely modelled on React with Hooks. React Components are called ElementComponent
s here (since Component
is already used by the ECS). There are a couple of concepts to keep track of here:
Entities
(React: DOM node) are just normal ECS entities, and they're kind of the "result" of all the work this module is doing.Element
(React:Element
). This is essentially a description of anEntity
or aElementComponent
. You can set normal ECS Components on it (for instance.set(translation(), ..)
). An Element can have children, which will be translated to achildren()
component on the Entity.ElementComponent
(React:Component
) is basically a function which takes some user inputs + the hooks (local state of an instance) and produces an Element tree from this.ElementComponent
s can wrap each other, so for instance an outerElementComponent
can return an Element which the innerElementComponent
only adds a Component to. In this case, there's only one Element, even though there are twoElementComponent
s. (i.e. there's not a 1:1 correspondence betweenElementComponent
s andElement
s).ElementInstance
(React:Instance
) is an instance of anElement
(remember,Element
s are just descriptions). It has a reference to the Entity it "owns". When using Hooks, the state is stored on theElementInstance
.ElementTree
is a tree ofElementInstance
s.
More info: